<?php
	header('content-type:text/html;charset=utf-8');
	date_default_timezone_set("Asia/Chongqing");
	require '../Class/autoload.php';

	// dump($_FILES);
	 //dump($_POST,1);


	if(I('db_host','p')){
		$db_host = IpDecrypt(I('db_host','p'));
	}
	if(I('cityCode','p')){
		$cityCode = I('cityCode','p');
		$db_name = 'shop_'.$cityCode;		//拼接库名 
	}


	//验证用户名
	$user = I('user','p');
	$user_arr = array(	//跳过验证的项
		'getOpenCityInfo',
		'storeRegister'
	);
	if(!in_array($_REQUEST['type'],$user_arr)){
		if(!$user){
			exit('10001');	//用户名不存在或异常
		}
	}


	$tmp_arr = array();		//返回数据集合
	
	//验证token
	$token_arr = array(		//跳过的项
		'getOpenCityInfo',
		'storeRegister',
		'storeLogin'
	);
	if(!in_array($_REQUEST['type'],$token_arr)){
		$token1 = I('token1');
		if(!VerifyToken1($user,$token1)){
			//exit('10002');
		}
	}



	$tokenv_arr = array(		//跳过获取token的项
		'getOpenCityInfo',
		'storeRegister'
	);
	if(!in_array($_REQUEST['type'],$tokenv_arr)){
		$tmp_arr['token1'] = SetToken1($user);
	}




	$db_arr = array(		//跳过检测db_host和db_name
		'getOpenCityInfo'
	);
	if(!in_array($_REQUEST['type'],$db_arr)){
		$db_host = IpDecrypt(I('db_host','p'));
		if(!$db_host || !$db_name){
			//echo $db_host.'--'.$db_name;
			exit('10003');		//缺少库址或库名
		}
	}



	//检测商户状态是否为正常状态
	function _checkStoreStat($cityCode,$user){
		if(!$user || !$cityCode){
			return false;
		}
		$statPath = '../Cache/storeState/'.$cityCode.'_'.$user;
		$storeStat = S($statPath);
		if($storeStat == 2){
			return true;
		}
		return false;
	}


	$storeStatArr = array(		//跳过商户状态判断的项
		'storeRegister',			//商户注册
		'getGoodsTypeListFull',		//获取商品类别列表
		'getGoodsListForStoreId',	//获取商品列表
		'getGoodsImgs', 			//获取商品图片列表
		'getGoodsInfo',				//获取商品信息
		'StoreInfoEdit',			//商户编辑
		'storeLogin',				//商户登陆
		'getFreeListForStoreId',	//获取商户下的砍价商品列表
		'getFreeOrderListForStore',	//获取商户下的砍价订单列表
		'getFreeOrderUser',			//获取砍价订单下的帮砍用户列表
		'findQuestion',				//商户找回密码
		'findPwd',					//商户找回登陆密码
		'updateStorePwd'			//商户修改密码


		,'addUser'			//临时 - 添加帮砍用户
		,'createFreeOrder'	//临时 - 添加砍价订单
	);
	if(!in_array($_REQUEST['type'],$storeStatArr)){
		if(!_checkStoreStat($cityCode,$user)){
			exit('10005');		//商户状态非正常
		}
	}




	switch ($_REQUEST['type']){

		//删除砍价商品
		case 'freeGoodsDel':
			$free_goods_id = intval(I('free_goods_id','p'));
			_checkVal($free_goods_id);

			//检查是否存有效期内的订单
			$tableShopFreeOrder = new TableShopFreeOrder();
			$res = $tableShopFreeOrder->_getFreeGoodsOrderCount($free_goods_id);
			if(@$res['count'] && $res['count'] > 0){
				$tmp_arr['stat'] = 'err2';	//还有未完成订单
				exit(json_encode($tmp_arr));
			}

			//关联删除砍价商品，砍价订单，帮砍用户数据
			$sql2 = "delete f,o,u from (shop_free f left join shop_free_order o on f.id = o.free_id) left join shop_free_order_user u on o.id = u.free_order_id where f.id = ?";

			$res2 = $tableShopFreeOrder->dml($sql2,array($free_goods_id));
			if($res2){
				$tmp_arr['stat'] = 'ok';
			}else{
				$tmp_arr['stat'] = 'err3';	//删除异常
			}
			exit(json_encode($tmp_arr));
		break;




		//临时添加虚拟帮砍用户
		case 'addUser':
			$order_id = I('order_id','p');
			$openid = 'openid_'.rand(1,99999);
			$name = 'name_'.rand(1,9999);
			$name = 'name_'.rand(1,9999);
			$header = '/shop_img/110101/a1a1/10/20200602211428.jpg';
			$price = round(rand(10,99)/10,2);
			$tableShopFreeOrderUser = new TableShopFreeOrderUser();
			$res = $tableShopFreeOrderUser->_FreeOrderUserAdd($order_id,$openid,$name,$header,$price);
			dump($res);
		break;




		//获取帮砍用户信息
		case 'getFreeOrderUser':
			$order_id = I('order_id','p');
			_checkVal($order_id);
			$tableShopFreeOrderUser = new TableShopFreeOrderUser();
			$res = $tableShopFreeOrderUser->_getFreeOrderUser($order_id);
			if($res){
				$tmp_arr['data'] = $res;
			}else{
				$tmp_arr['data'] = array();
			}
			exit(json_encode($tmp_arr));
		break;



		//获取指定商户的砍价订单
		case 'getFreeOrderListForStore':
			$store_id = intval(I('store_id','p'));
			$selType = intval(I('selType','p'));
			$search = I('search','p');
			$freeGoodsId = intval(I('freeGoodsId','p'));	//查询指定砍价商品下的关联订单时使用

			_checkVal($store_id,$selType);
			$tableShopFreeOrder = new TableShopFreeOrder();
			$res = $tableShopFreeOrder->_getFreeOrderListForStore($store_id,$selType,$search,$freeGoodsId);
			if($res){
				$tmp_arr['data'] = $res;
			}else{
				$tmp_arr['data'] = array();
			}
			exit(json_encode($tmp_arr));
		break;



		//临时测试：创建砍价订单
		case 'createFreeOrder':
			$free_id = intval(I('free_id','p'));
			$store_id = intval(I('store_id','p'));
			$price = floatval(I('price','p'));
			_checkVal($free_id,$store_id,$price);
			$tableShopFreeOrder = new TableShopFreeOrder();
			$tmp_arr['stat'] = $tableShopFreeOrder->_createFreeOrder($free_id,$price,$store_id);
			exit(json_encode($tmp_arr));
		break;






		//砍价商品的上下架
		case 'changeFreeGoodsStat':
			$free_id = intval(I('free_id','p'));
			$free_stat = intval(I('free_stat','p'));
			if($free_stat == 3){
				$tmp_arr['stat'] = 'err3';		//封禁状态下不能操作
				exit(json_encode($tmp_arr));
			}
			$free_stat = $free_stat==1?2:1;
			_checkVal($free_id,$free_stat);
			$tableShopFree = new TableShopFree();
			$res = $tableShopFree->_changeFreeGoodsStat($free_id,$free_stat);
			if($res){
				$tmp_arr['stat'] = 'ok';
			}else{
				$tmp_arr['stat'] = 'err2';
			}
			exit(json_encode($tmp_arr));
		break;



		//通过商户ID获取砍价商品列表
		case 'getFreeListForStoreId':
			$store_id = intval(I('store_id','p'));
			_checkVal($store_id);

			$tableShopFree = new TableShopFree();
			$res = $tableShopFree->_getFreeListForStoreId($store_id);
			if($res){
				$tmp_arr['data'] = $res;
			}else{
				$tmp_arr['data'] = array();
			}
			exit(json_encode($tmp_arr));
		break;





		//添加砍价商品
		case 'freeGoodsAdd':
			$store_id = intval(I('store_id','post'));
			$goods_id = intval(I('goods_id','post'));
			$goods_stat = intval(I('goods_stat','post'));
			_checkVal($store_id,$goods_id,$goods_stat);
			if($goods_stat != 2){
				$tmp_arr['stat'] = 'err3';		//商品还未通过审核
				exit(json_encode($tmp_arr));
			}

			$tableShopFree = new TableShopFree();
			$res = $tableShopFree->_freeGoodsAdd($store_id,$goods_id);
			if($res){
				$tmp_arr['stat'] = 'ok';
			}else{
				$tmp_arr['stat'] = 'err2';	//操作异常或砍价商品已存在 
			}
			exit(json_encode($tmp_arr));
			break;








		//商品的删除
		case 'storeGoodsDel':
			$imgDirName = I('imgDirName','post');
			$goods_id = intval(I('goods_id','post'));
			$store_id = intval(I('store_id','post'));
			_checkVal($goods_id,$store_id,$imgDirName);

			//删除商品关联的图片和目录
			$tableShopImg = new TableShopImg();
			$tableShopImg->_goodsImgsDel($goods_id,$imgDirName);

			//删除商品数据
			$tableShopGoods = new TableShopGoods();
			$res = $tableShopGoods->_storeGoodsDel($goods_id,$store_id);
			if($res){
				$tmp_arr['stat'] = 'ok';
			}else{
				$tmp_arr['stat'] = 'err2';
			}
			exit(json_encode($tmp_arr));
			break;





		//商品的上架下架
		case 'goodsUpOrDown':
			$id = intval(I('id','post'));
			$stat = intval(I('stat','post'));
			_checkVal($id,$stat);
			$tableShopGoods = new TableShopGoods();
			$res = $tableShopGoods->_goodsUpOrDown($id,$stat);
			if($res){
				$tmp_arr['stat'] = 'ok';
			}else{
				$tmp_arr['stat'] = 'err';
			}
			exit(json_encode($tmp_arr));
			break;





		//删除商品下的图片
		case 'goodsImgDel':
			$img_id = intval(I('img_id','p'));
			$img_addr = I('img_addr','p');
			$goods_id = intval(I('goods_id','p'));
			_checkVal($img_id,$img_addr,$goods_id);
			$tableShopImg = new TableShopImg();
			$res = $tableShopImg->_goodsImgDel($img_id,$img_addr,$goods_id);
			if($res){
				$tmp_arr['stat'] = 'ok';
				$tableShopGoods = new TableShopGoods();	//更改商品状态
				$tableShopGoods->_resetGoodsStat($goods_id);
			}else{
				$tmp_arr['stat'] = 'err2';
			}
			exit(json_encode($tmp_arr));
		break;




		//获取商品的图片列表
		case 'getGoodsImgs':
			$goods_id = intval(I('goods_id','post'));
			$tableShopImg = new TableShopImg();
			$res = $tableShopImg->_getImgListForGid($goods_id);
			if($res){
				$tmp_arr['data'] = $res;
			}else{
				$tmp_arr['data'] = array();
			}
			exit(json_encode($tmp_arr));
			break;









		//获取商品信息
		case 'getGoodsInfo':
			$goods_id = intval(I('goods_id','post'));
			$tableShopGoods = new TableShopGoods();
			$res = $tableShopGoods->_getGoodsInfoForId($goods_id);
			if($res){
				$tmp_arr['stat'] = 'ok';
				$tmp_arr['data'] = $res;
			}else{
				$tmp_arr['stat'] = 'err2';
			}
			exit(json_encode($tmp_arr));
			break;



		//商品添加图片
		case 'goodsImageAdd':
			$goods_id = intval(I('goods_id','post'));
			$store_id = intval(I('store_id','post'));
			if(!$goods_id || !$store_id){
				exit('err5');
			}
			$tableShopImg = new TableShopImg();
			$tmp_arr['stat'] = $tableShopImg->_goodsImgAdd($cityCode,$goods_id,$user);
			if($tmp_arr['stat'] == 'ok'){
				$tableShopGoods = new TableShopGoods();	//更改商品状态
				$tableShopGoods->_resetGoodsStat($goods_id);
			}
			exit(json_encode($tmp_arr));
			break;



		//商品的编辑
		case 'storeGoodsEdit':
			$goods_id = intval(I('goods_id','post'));
			$t1 = I('t1','post');
			$t2 = I('t2','post');
			$t3 = I('t3','post');
			$type_set = I('type_set','post');
			if(!$t1 || !$t2 || !$t3 || !$type_set){
				$tmp_arr['stat'] = 'err3';
				exit(json_encode($tmp_arr));
			}

			$banner = @$_POST['banner'];

			
			//after-商品名1-50个字符!
			$title = I('title','post');
			if(!Field_Check('/^.{1,50}$/',$title)){
				$tmp_arr['stat'] = 'err4';
				exit(json_encode($tmp_arr));
			}

			//after-编号为0-4四位数字!
			$number = I('number','post')?:'';
			if(!Field_Check('/^[0-9]{1,4}$|^$/',$number)){
				$tmp_arr['stat'] = 'err5';
				exit(json_encode($tmp_arr));
			}

			//after-价格不合法!
			$price = I('price','post');
			if(!Field_Check('/^[1-9][0-9]{0,8}(\.[0-9]{2})?|0\.[0-9]{2}?$/',$price)){
				$tmp_arr['stat'] = 'err6';
				exit(json_encode($tmp_arr));
			}


			//after-商品详情不得为空!
			$info = I('info','post');
			if(!Field_Check('/^.{1,1000}$/',$info)){
				$tmp_arr['stat'] = 'err7';
				exit(json_encode($tmp_arr));
			}

			$tableShopGoods = new TableShopGoods();
			$res = $tableShopGoods->_storeGoodsEdit($goods_id,$t1,$t2,$t3,$type_set,$banner,$title,$number,$price,$info);
			if($res){
				$tmp_arr['stat'] = 'ok';
			}else{
				$tmp_arr['stat'] = 'err2';
			}
			exit(json_encode($tmp_arr));
			break;






		//商品的添加
		case 'storeGoodsAdd':
			// if(!_checkStoreStat($cityCode,$user)){
			// 	$tmp_arr['stat'] = 'errStat';	//商户状态异常
			// 	exit(json_encode($tmp_arr));
			// }
			$t1 = I('t1','post');
			$t2 = I('t2','post');
			$t3 = I('t3','post');
			$typeSet = I('typeSet','post');
			$store_id = I('store_id','post');

			if(!$t1 || !$t2 || !$t3 || !$store_id || !$typeSet){
				exit('err3');	//缺少参数！
			}

			$title = I('title','post');
			Field_Check('/^.{1,50}$/',$title,'err4'); 	//after-商品名1-50个字符!

			$number = I('number','post')?:'';
			Field_Check('/^[0-9]{1,4}$|^$/',$number,'err5'); 	//after-编号为0-4四位数字!

			$price = I('price','post');
			Field_Check('/^[1-9][0-9]{0,8}(\.[0-9]{2})?$/',$price,'err6'); //after-价格不合法!

			$info = I('info','post');
			Field_Check('/^.{1,}$/',$info,'err7'); 	//after-商品详情不得为空!


			$tableShopGoods = new TableShopGoods();
			$tmp_arr['stat'] = $tableShopGoods->_storeGoodsAdd($store_id,$t1,$t2,$t3,$typeSet,$title,$number,$price,$info);
			exit(json_encode($tmp_arr));
		break;







		//获取商品类型全部数据
		case 'getGoodsTypeListFull':
			$tmp_arr['data'] = _getGoodsTypeListFull();
			exit(json_encode($tmp_arr));
		break;



		//获取商户下商品列表
		case 'getGoodsListForStoreId':

			$store_id = intval(I('store_id','p'));
			$tableShopGoods = new TableShopGoods();
			$res = $tableShopGoods->_getGoodsListForStoreId($store_id,'id,banner,title,price,goods_stat,number,type_set,is_free');
			if($res){
				$data = $res;
			}else{
				$data = array();
			}
			$tmp_arr['stat'] = 'ok';
			$tmp_arr['data'] = $data;
			exit(json_encode($tmp_arr));
		break;




		//更改商户信息
		case 'StoreInfoEdit':
			$tableShopStore = new TableShopStore();
			$tableShopStore->_updateStoreInfo($tmp_arr);
			if($tmp_arr['stat'] == 'ok'){
				$statePath = '../Cache/storeState/'.$cityCode.'_'.$user;
				S($statePath,1);
			}
			exit(json_encode($tmp_arr));
		break;



		//商户修改密码
		case 'updateStorePwd':
			$p1 = I('p1','p');
			$p2 = I('p2','p');
			_checkVal($p1,$p2);
			$tableShopStore = new TableShopStore();
			$res = $tableShopStore->_updateStorePwd($user,$p1,$p2);
			if($res){
				$tmp_arr['stat'] = 'ok';
			}else{
				$tmp_arr['stat'] = 'err2';
			}
			exit(json_encode($tmp_arr));
		break;




		//判断密保，并找回密码
		case 'findPwd':
			$answer = I('answer','p');
			$tableShopStore = new TableShopStore();
			$res = $tableShopStore->_findPwd($user,$answer);
			exit("$res");
		break;




		//商户找回登陆密码
		case 'findQuestion':
			$tableShopStore = new TableShopStore();
			$res = $tableShopStore->_findQuestion($user);
			if($res){
				exit($res['question']);
			}else{
				exit('err');
			}
		break;



		//商户注册
		case 'storeRegister':

			$tableShopStore = new TableShopStore();
			$res = $tableShopStore->_createStore();
			exit($res);
		break;









		//商户登陆
		case 'storeLogin':
			$statePath = '../Cache/storeState/'.$cityCode.'_'.$user;
			$pwd = md5(I('pwd','p'));
			$tableShopStore = new TableShopStore();
			$res = $tableShopStore->_getStoreInfo($user);
			if($res && $res['pwd'] == $pwd){
				$tmp_arr['stat'] = 'ok';
				unset($res['pwd']);
				unset($res['question']);
				unset($res['answer']);
				$tmp_arr['data'] = $res;
				S($statePath,$res['stat']);	//缓存商户状态，以便后续操作时判断商户状态
			}else{
				$tmp_arr['stat'] = 'err2';
			}
			exit(json_encode($tmp_arr));
		break;









		//获取开通城市信息
		case 'getOpenCityInfo':
			$tmp_arr['data'] = getOpenCityInfo();
			exit(json_encode($tmp_arr));
		break;












	// 	//遍历所有县库并删除商品的过期的图片(数据库及upyun)
	// 	case 'clear_img':
	// 		$ourl = 'http://shop-cache-upyun.bm123.top/CityInfo/CityOpen.html';
	// 		$res = CurlForGet($ourl);
	// 		if(preg_match('/^Gets\(.*\)$/',$res)){
	// 			preg_match_all('/\[.*\]/', $res, $res2);
	// 			if($res2[0][0]){
	// 				$res3 = json_decode($res2[0][0],1);
	// 				foreach($res3 as $v){
	// 					$ip = IpDecrypt($v['db_host']);
	// 					$pdo = MyPdo::init($ip,$v['db_name'],$config['county_db_user'],$config['share_db_pwd'],true);
	// 					$sql = 'select id,addr from shop_img where gid=0 and fid=0 and expire < ?';
	// 					$res4 = $pdo->dql($sql,array(time()- 10));
	// 					if($res4){
	// 						foreach($res4 as $v){
	// 							$res5 = UpyunDelFile($config,$v['addr']);
	// 							if($res5){
	// 								$sql2 = 'delete from shop_img where id = ?';
	// 								$res6 = $pdo->dml($sql2,array($v['id']));
	// 								dump($res6);
	// 							}
	// 						}
	// 					}
	// 				}
	// 			}
	// 		}
	// 		exit;
	// 	break;

	// 	//关闭或封禁砍价商品
	// 	case 'FreeGoodsClose':
	// 		$free_id = intval(I('free_id','post'));
	// 		$c_type = I('c_type','post');	//有值为封禁，无值为关闭
	// 		if(!$free_id){
	// 			exit('err1');
	// 		}
	// 		$pdo = MyPdoCounty::init();
	// 		if($c_type){ 	//封禁
	// 			$stat = 3;
	// 		}else{ 			//关闭
	// 			$stat = 2;
	// 		}
	// 		$sql = 'update shop_free set free_stat = '.$stat.' where id=?';
	// 		$res = $pdo->dml($sql,array($free_id));
	// 		if($res){
	// 			exit('ok');
	// 		}else{
	// 			exit('err');
	// 		}
	// 		break;



	// 	//删除砍价商品
	// 	case 'FreeGoodsDel':
	// 		$free_id = intval(I('free_id','post'));
	// 		//$store_id = intval(I('store_id','post'));
	// 		$banner = $_POST['banner'];
	// 		if(!$free_id || !$banner){
	// 			exit('err1');
	// 		}
	// 		$pdo = MyPdoCounty::init();
	// 		if($banner){
	// 			$sql3 = 'update shop_img set fid = if(fid-1>0,fid-1,0),expire = ? where addr = ?';
	// 			$res3 = $pdo->dml($sql3,array(time(),$banner));
	// 		}
	// 		$sql = 'delete from shop_free where id = ?';
	// 		$res = $pdo->dml($sql,array($free_id));
	// 		if($res){
	// 			exit('ok');
	// 		}else{
	// 			exit('err1');	//删除砍价商品失败
	// 		}
	// 		break;


	// 	//获取砍价商品列表 
	// 	case 'FreeGoodsGetList':
	// 		$store_id = intval(I('store_id','post'));
	// 		$pdo = MyPdoCounty::Init();
	// 		$sql = "select id,title,price,banner,free_stat from shop_free where store_id = ?";
	// 		$res = $pdo->dql($sql,array($store_id));
	// 		if($res){
	// 			echo json_encode($res);
	// 		}else{
	// 			exit('err');
	// 		}
	// 		break;


	// 	//添加砍价商品
	// 	case 'FreeGoodsAdd':
	// 		$store_id = intval(I('store_id','post'));
	// 		$goods_id = intval(I('goods_id','post'));
	// 		if(!$store_id || !$goods_id){
	// 			exit('err1');
	// 		}
	// 		$pdo = MyPdoCounty::init();

	// 		$sql1 = 'select title,price,banner from shop_goods where id = ?';
	// 		$res1 = $pdo->dql($sql1,array($goods_id),1);
	// 		if($res1){
	// 			$sql2 = 'insert into shop_free(store_id,title,price,banner,ctime) values(?,?,?,?,?)';
	// 			$res2 = $pdo->dml($sql2,array($store_id,$res1['title'],$res1['price'],$res1['banner'],time()));
	// 			if($res2){
	// 				//修改商品对应的free_id
	// 				//$sql3 = 'update shop_goods set free_id = ? where id = ?';
	// 				//$res3 = $pdo->dml($sql3,array($res2,$goods_id));
	// 				//$sql4 = 'update shop_img set fid = ? where addr = ?';
	// 				//$res4 = $pdo->dml($sql4,array($res2,$res1['banner']));
	// 				//修改图片表中free项值 
	// 				$sql4 = 'update shop_img set fid = fid+1 where addr = ?';
	// 				$res4 = $pdo->dml($sql4,array($res1['banner']));
	// 				exit('ok');
	// 			}
	// 		}
	// 		break;




	// 	//获取指定商户下未关联且常态的商品列表
	// 	case 'GetNormalGoodsList';
	// 		$store_id = intval(I('store_id','post'));
	// 		$pdo = MyPdoCounty::init();
	// 	//	$sql = "select id,title,price,number from shop_goods where store_id=? and goods_stat=2 and free_id=0";
	// 		$sql = "select id,title,price,number from shop_goods where store_id=? and goods_stat=2 and store_stat = 1";
	// 		$res = $pdo->dql($sql,array($store_id));
	// 		if($res){
	// 			echo json_encode($res);
	// 		}else{
	// 			exit('err');
	// 		}
	// 		break;




	// 	//商品删除指定单张图片
	// 	case 'GoodsImageDel':
	// 		$img_id = I('img_id','post');
	// 		$goods_id = I('goods_id','post');
	// 		if(!$img_id || !$goods_id){
	// 			exit('err1');
	// 		}
	// 		$pdo = MyPdoCounty::init();
	// 		$sql1 = 'update shop_img set gid = 0,expire=? where id = ? and gid = ?';
	// 		$res1 = $pdo->dml($sql1,array(time(),$img_id,$goods_id));
	// 		if($res1){
	// 			if(!@$_SESSION['img_update_stat'] || $_SESSION['img_update_stat']+5 < time()){
	// 				$sql2 = 'update shop_goods set goods_stat = 1,utag1=utag1+1,utag3=utag3+1 where id = ?';
	// 				$res2 = $pdo->dml($sql2,array($goods_id));
	// 				$_SESSION['img_update_stat'] = time();
	// 			}

	// 			if(@$_SESSION['GoodsImageAdd_Count_'.$goods_id]){
	// 				unset($_SESSION['GoodsImageAdd_Count_'.$goods_id]);
	// 			}
	// 			exit('ok');
	// 		}else{
	// 			exit('err');
	// 		}
	// 		break;



	// 	//商品添加图片
	// 	case 'GoodsImageAdd':
	// 		$goods_id = intval(I('goods_id','post'));
	// 		$store_id = intval(I('store_id','post'));
	// 		if(!$goods_id || !$store_id){
	// 			exit('err5');
	// 		}
	// 		$pdo = MyPdoCounty::init();
	// 		if(@$_SESSION['GoodsImageAdd_Count_'.$goods_id]){
	// 			$count = $_SESSION['GoodsImageAdd_Count_'.$goods_id] + 1;
	// 		}else{
	// 			$sql1 = "select count(gid) as c from shop_img where gid = ?";
	// 			$res1 = $pdo->dql($sql1,array($goods_id),1);
	// 			if($res1){
	// 				if($res1['c'] > 0){
	// 					$count = $res1['c'];
	// 				}else{
	// 					$count = 1;
	// 				}
	// 			}else{
	// 				exit('err4');	//查询总条数异常
	// 			}
	// 		}
	// 		if($count <= 50){		//判断商品图片是否超过20张
	// 			$file = $_FILES['img'];
	// 			$full_path = '/shop_img/'.$_SESSION['CountyInfo'][0].'/'.$store_id.'/'.$goods_id.'/'.date('YmdHis').'.jpg';
	// 			$res_upyun = UploadImgToUpyun($config,$full_path,$file,1024*300);
	// 			if(is_array($res_upyun)){
	// 				$sql4 = "insert into shop_img(gid,addr) values(?,?)";
	// 				$res4 = $pdo->dml($sql4,array($goods_id,$full_path));
	// 				if($res4){
	// 					if(!@$_SESSION['img_update_stat'] || $_SESSION['img_update_stat']+5 < time()){
	// 						$sql3 = "update shop_goods set goods_stat=1,utag1=utag1+1,utag3=utag3+1 where id=?";
	// 						$res3 = $pdo->dml($sql3,array($goods_id));
	// 						$_SESSION['img_update_stat'] = time();
	// 					}
	// 					$_SESSION['GoodsImageAdd_Count_'.$goods_id] = $count;	//计数
	// 					exit('ok');
	// 				}else{
	// 					UpyunDelFile($config,$full_path);
	// 					echo 'err3';  //图片上传成功，但数据写入失败
	// 				}
	// 			}else{
	// 				exit('err2'); //文件上传失败
	// 			}
	// 		}else{
	// 			echo 'err1'; 	//图片数量超限!
	// 		}
	// 		break;


	// 	//获取商品的图片列表
	// 	case 'GetImgList':
	// 		$gid = intval(I('gid','post'));
	// 		$pdo = MyPdoCounty::init();
	// 		$sql = "select id,addr from shop_img where gid = ?";
	// 		$res = $pdo->dql($sql,array($gid));
	// 		if($res){
	// 			echo json_encode($res);
	// 		}
	// 		break;





	// 	//获取商品列表
	// 	case 'GetGoodsList':
	// 		$store_id = intval(I('store_id','post'));
	// 		$pdo = MyPdoCounty::init();
	// 		$fields = 'id,title,price,number,banner,goods_stat,store_stat';
	// 		$sql = "select {$fields} from shop_goods where store_id = ? and goods_stat != 6 order by id desc";
	// 		$res = $pdo->dql($sql,array($store_id));
	// 		if($res){
	// 			echo json_encode($res);
	// 		}
	// 		//id,store_id,openid,tid1,tid2,tid3,title,price,info,number,banner,hit,ctime,goods_stat,store_stat,free_id 
	// 		break;



	// 	//获取商品信息
	// 	case 'GetGoodsInfo':
	// 		$id = intval(I('id','post'));
	// 		$pdo = MyPdoCounty::init();
	// 		$fields = 'id,tid1,tid2,tid3,type_set,title,price,info,number,banner';
	// 		$sql = "select {$fields} from shop_goods where id = ?";
	// 		$res = $pdo->dql($sql,array($id),1);
	// 		if($res){
	// 			echo json_encode($res);
	// 		}
	// 		break;



	// 	//商品的上架下架
	// 	case 'ChangeGoodsStat':
	// 		$id = intval(I('id','post'));
	// 		$stat = intval(I('stat','post'));
	// 		$pdo = MyPdoCounty::init();
	// 		$sql = 'update shop_goods set goods_stat = ? where id = ?';
	// 		$res = $pdo->dml($sql,array($stat,$id));
	// 		if($res){
	// 			EditStoreUtag3($pdo,$id);	//修改商户下的商品列表版本utag3
	// 			//还要对商品utag1和upyun做更新
				
	// 			exit('ok');
	// 		}else{
	// 			exit('err');
	// 		}
	// 		//注：上下架时，立即更新商户的utag3
	// 		break;



	// 	//商品的删除
	// 	case 'StoreGoodsDel':
	// 		$id = intval(I('id','post'));
	// 		$pdo = MyPdoCounty::init();

	// 		$sql3 = 'select count(gid) as count from shop_img where gid = ?';
	// 		$res3 = $pdo->dql($sql3,array($id),1);
	// 		if($res3 && $res3['count'] > 0){
	// 			//先解除商品关联的图片
	// 			$sql = 'update shop_img set gid = 0,expire=? where gid = ?';
	// 			$res = $pdo->dml($sql,array(time(),$id));
	// 			if(!$res){
	// 				exit('err');
	// 			}
	// 		}
	// 		//将商品置为失效状态
	// 		$sql2 = "update shop_goods set title='',info='',banner='',type_set='',ctime='',utag1=0,utag3=0,goods_stat = 6 where id = ?";
	// 		$res2 = $pdo->dml($sql2,array($id));
	// 		if($res2){

	// 			EditStoreUtag3($pdo,$id);	//修改商户下的商品列表版本utag3

	// 			//缓存数据到upyun
	// 			//$upyun_path_update = '/detail_goods/dg_'.$_SESSION['CountyInfo'][0].'_'.$id.'.html';
	// 			//UpyunAddFile($config,$upyun_path_update,'[]');

	// 			$cache_name = '/detail_goods/dg_'.$_SESSION['CountyInfo'][0].'_'.$id;
	// 			$cache_path = $cache_name.'.html';
	// 			$cache_tag_path = $cache_name.'_utag1.html';
	// 			UpyunAddFile($config,$cache_path,'[]');
	// 			UpyunAddFile($config,$cache_tag_path,'none');
	// 			exit('ok');
	// 		}else{
	// 			exit('err2');
	// 		}

	// 		//注：删除时，立即更新商户的utag3
	// 		break;





	// 	//商品的编辑
	// 	case 'StoreGoodsEdit':
			
	// 		$id = I('id','post');
	// 		$code = I('code','post');
	// 		Field_Check('/^.{1,}$/',$code,'err1');	//after-验证码不得为空!
	// 		if(!CheckCode($code)){
	// 			//exit('err2');	//验证码不正确！
	// 		}

	// 		$t1 = I('t1','post');
	// 		$t2 = I('t2','post');
	// 		$t3 = I('t3','post');
	// 		$type_set = I('type_set','post');
	// 		if(!$t1 || !$t2 || !$t3 || !$type_set){
	// 			exit('err3');	//缺少参数！
	// 		}

	// 		$banner = @$_POST['banner'];

	// 		$title = I('title','post');
	// 		Field_Check('/^.{1,50}$/',$title,'err4'); 	//after-商品名1-50个字符!

	// 		$number = I('number','post')?:'';
	// 		Field_Check('/^[0-9]{1,4}$|^$/',$number,'err5'); 	//after-编号为0-4四位数字!

	// 		$price = I('price','post');
	// 		Field_Check('/^[1-9][0-9]{0,8}(\.[0-9]{2})?$/',$price,'err6'); //after-价格不合法!

	// 		$info = I('info','post');
	// 		Field_Check('/^.{1,}$/',$info,'err7'); 	//after-商品详情不得为空!

	// 		$pdo = MyPdoCounty::init();
	// 		$sql = 'update shop_goods set tid1=?,tid2=?,tid3=?,type_set=?,title=?,number=?,price=?,info=?,banner=?,goods_stat=1,utag1=utag1+1 where id=?';
	// 		$res = $pdo->dml($sql,array($t1,$t2,$t3,$type_set,$title,$number,$price,$info,$banner,$id));
	// 		if($res){
	// 			exit('ok');
	// 		}else{
	// 			exit('err4');
	// 		}

	// 		//注：在后台管理通过时才对该商户的utag3做更新
	// 		break;








	// /*	//全局服获取商品类别列表
	// 	case 'GetGoodTypes':
	// 		$pdo = MyPdoShare::init(1);
	// 		$sql = "select * from share_goods_type";
	// 		$res = $pdo->dql($sql,array());
	// 		echo json_encode($res);
	// 		break;*/


	// 	//将商户的县库信息缓存到session
	// 	case 'SetStoreAreaInfoToSession':
	// 		$city_code = I('city_code','post');
	// 		$db_host = I('db_host','post');
	// 		$db_name = I('db_name','post');
	// 		if(!$city_code || !$db_host || !$db_name){
	// 			exit('err');
	// 		}
	// 		$arr[] = $city_code;
	// 		$arr[] = $db_host;
	// 		$arr[] = $db_name;
	// 		$_SESSION['CountyInfo'] = $arr;
	// 		if(count($_SESSION['CountyInfo']) >=3){
	// 			exit('ok');
	// 		}else{
	// 			exit('err2');
	// 		}
	// 		break;



	// 	//获取商户信息
	// 	case 'GetStoreInfo':
	// 		$openid = I('openid','post');
	// 		if(!$openid){
	// 			exit('err1');	//缺少openid
	// 		}

	// 		//向全局库获取商户的库址记录
	// 		$pdo = MyPdoShare::init(1);
	// 		//### 注 , 将来同时需要在upyun上做静态缓存 
	// 		$sql = "select city_code,db_host,db_name from share_openid_server where openid = ?";
	// 		$res = $pdo->dql($sql,array($openid),1);
	// 		if($res){	//获取到县库址
	// 			if(count($res) < 3){
	// 				exit('err2');		//县服参数异常
	// 			}
	// 			foreach($res as $v){
	// 				$city_info[] = $v;
	// 			}
	// 			$_SESSION['CountyInfo'] = $city_info;

	// 			//向县库获取商户数据
	// 			$pdo2 = MyPdoCounty::init(1);
	// 			$sql2 = "select * from shop_store where openid = ?";
	// 			$res2 = $pdo2->dql($sql2,array($openid),1);
	// 			if($res2){
	// 				echo $store_info = json_encode($res2);

	// 				//缓存商户验证码
	// 				$v_code = SetFieldsVCode($res2['id'],$res2['name'],$_SESSION['CountyInfo'][1],$_SESSION['CountyInfo'][2]);
	// 				$_SESSION['StoreVCode'] = $v_code;

	// 				//向upyun做商户状态标记缓存
	// 				$store_info2 = 'Gets('.$store_info.')';
	// 				$path = '/Openid/'.$openid.'.html';
	// 				UpyunAddFile($config,$path,$store_info2);
	// 			}else{
	// 				exit('err3'); 	//全局库有记录，但全局缓存县服无商户信息，数据丢失
	// 			}
	// 		}else{	//全局服没有记录，跳转到注册页
	// 			exit('err4');
	// 		}
	// 		break;



	// 	//绑定注册或编辑商户信息
	// 	case 'StoreInfoAddOrEdit':
	// 		$code = I('code','post'); 
	// 		if(!CheckCode($code)){
	// 			//exit('err1');		//验证码不正确
	// 		}
	// 		$openid = I('openid','post');
	// 		if(!$openid){
	// 			exit('err2');	//缺少openid
	// 		}


	// 		//如果是注册，需要城市信息
	// 		if(@$_POST['county']){
	// 			$county = I('county','post'); 
	// 			$countys = explode(',',$county);
	// 			if(count($countys) < 3){
	// 				exit('err3');	//城区信息异常
	// 			}
	// 			$_SESSION['CountyInfo'] = $countys;
	// 			$city_code = $countys[0];
	// 			$db_host   = $countys[1];
	// 			$db_name   = $countys[2];

	// 			$CityFullName = I('CityFullName','post'); 
	// 		}else{  	//如果是修改
	// 			$county = I('city_val','post'); 
	// 			$countys = explode(',',$county);
	// 			if(count($countys) < 3){
	// 				exit('err3');	//区域信息异常
	// 			}
	// 			$_SESSION['CountyInfo'] = $countys;
	// 		}

	// 		//echo $county;exit;

	// 		$name = I('name','post'); 
	// 		Field_Check('/^.{3,20}$|^[\x{4e00}-\x{9fa5}]{3,20}$/u',$name,'商户名长度为3-20个字符!','Void_Token');

	// 		$phone = I('phone','post'); 
	// 		Field_Check('/^[1-9][0-9]{6,11}$/',$phone,'电话格式不正确!','Void_Token');

	// 		$qq = I('qq','post')?:'';
	// 		Field_Check('/^[1-9][0-9]{4,11}$|^$/',$qq,'QQ号不正确!','Void_Token');

	// 		$wx = I('wx','post')?:''; 
	// 		Field_Check('/^[a-zA-z0-9_-]{3,30}$|^$/',$wx,'微信号格式不正确!','Void_Token');

	// 		$addr = I('addr','post')?:''; 
	// 		Field_Check('/^.{1,50}$|^[\x{4e00}-\x{9fa5}]{1,50}$|^$/u',$addr,'地址长度不超过50个字符!','Void_Token');

	// 		$info = I('info','post'); 
	// 		Field_Check('/^.{1,300}$|^[\x{4e00}-\x{9fa5}]{1,300}$|^$/u',$info,'商户描述不得为空且不得超过300字符!','Void_Token');


	// 		$banner = '';

	// 		if($_FILES){
	// 			$imgs = $_FILES['banner'];
	// 			$full_path = '/StoreBanner/'.$countys[0].'/'.$openid.'_banner.jpg'; 	
	// 			//上传图片到upyun ,检测 ，缩放组合函数
	// 			$res_upyun = UploadImgToUpyun($config,$full_path,$imgs,1024*200);
	// 			if(is_array($res_upyun)){
	// 				$banner = $full_path.'?t='.rand(1,9999);
	// 			}else{
	// 				exit('err5'); //文件上传失败
	// 			}
	// 		}
	// 		$pdo = MyPdoCounty::init();
	// 		$sql = "select banner,stat from shop_store where openid = ?";
	// 		$res = $pdo->dql($sql,array($openid),1);
	// 		if($res){ 	//修改商户信息
	// 			if($res['stat'] == 4){
	// 				exit('err6');	//商户已封禁
	// 			}
	// 			if($banner){	//判断编辑时是否修改了封面
	// 				$sql2 = "update shop_store set stat = 1,name=?,addr=?,info=?,phone=?,wx=?,qq=?,banner=?,utag1=utag1+1 where openid=?";
	// 				$sql_arr = array($name,$addr,$info,$phone,$wx,$qq,$banner,$openid);
	// 			}else{
	// 				$sql2 = "update shop_store set stat = 1,name=?,addr=?,info=?,phone=?,wx=?,qq=?,utag1=utag1+1 where openid=?";
	// 				$sql_arr = array($name,$addr,$info,$phone,$wx,$qq,$openid);
	// 			}
	// 			$res2 = $pdo->dml($sql2,$sql_arr);
	// 			if($res2){
	// 				$sql4 = 'select * from shop_store where openid = ?';
	// 				$res4 = $pdo->dql($sql4,array($openid),1);
	// 				if($res4){
	// 					echo $res6 = json_encode($res4);


	// 					//缓存商户信息验证码
	// 					$v_code = SetFieldsVCode($res4['id'],$res4['name'],$_SESSION['CountyInfo'][1],$_SESSION['CountyInfo'][2]);
	// 					$_SESSION['StoreVCode'] = $v_code;


	// 					$res5 = 'Gets('.$res6.')';
	// 					$path = '/Openid/'.$openid.'.html';
	// 					//缓存商户信息到全局服
	// 					UpyunAddFile($config,$path,$res5); 
	// 				}else{
	// 					echo 'err9'; //修改成功后获取商户信息失败
	// 				}
	// 			}else{
	// 				exit('err7');	//数据没有任何改动
	// 			}
	// 		}else{ 	//注册商户信息
	// 			$sql3 = "insert into shop_store values(null,?,?,?,?,?,?,?,?,0,1,?,?,?,?,?,0,0,0,0,0)";
	// 			$id_new = $pdo->dml($sql3,array($openid,$name,$addr,$info,$phone,$wx,$qq,$banner,time(),$city_code,$db_host,$db_name,$CityFullName));
	// 			if($id_new){
	// 				//向全局服缓存openid及县服信息
	// 				$pdo_s = MyPdoShare::init(1);
	// 				$sql_s = 'insert ignore into share_openid_server values(null,?,?,?,?)';
	// 				$res_s = $pdo_s->dml($sql_s,array($openid,$city_code,$db_host,$db_name));
	// 				if($res_s){
	// 					//记录到全局服后，获取注册好的信息，
	// 					$sql4 = 'select * from shop_store where id=?';
	// 					$res4 = $pdo->dql($sql4,array($id_new),1);
	// 					if($res4){
	// 						echo $res6 = json_encode($res4);

	// 						//缓存商户信息验证码
	// 						$v_code = SetFieldsVCode($res4['id'],$res4['name'],$_SESSION['CountyInfo'][1],$_SESSION['CountyInfo'][2]);
	// 						$_SESSION['StoreVCode'] = $v_code;

	// 						$res5 = 'Gets('.$res6.')';
	// 						$path = '/Openid/'.$openid.'.html';
	// 						//缓存商户信息到全局服
	// 						UpyunAddFile($config,$path,$res5); 
	// 					}else{
	// 						$err_info = "县服异常通知\r\nopenid：".$openid."\r\n商户id：".$id_new."\r\ninfo："."\r\n地区码：".$city_code."\r\n库址：".IpDecrypt($db_host)."\r\n库名：".$db_name;
	// 						Wx_Template_Notice('注册后获取商户信息失败！',array($err_info,date('Y-m-d H:i:s')));
	// 						exit('err8');	//获取商户信息失败
	// 					}
	// 				}else{
	// 					$err_info = "全局服异常通知\r\nopenid：".$openid."\r\n商户id：".$id_new."\r\ninfo："."\r\n地区码：".$city_code."\r\n库址：".IpDecrypt($db_host)."\r\n库名：".$db_name;
	// 					Wx_Template_Notice('商户openid全局缓存失败！',array($err_info,date('Y-m-d H:i:s')));
	// 					exit('err9');	//注册成功，向全局服记录商户对应县服信息失败
	// 				}
	// 			}else{
	// 				exit('err10');	//注册失败
	// 			}
	// 		}

	// 		break;







	// 	//向全局服获取城市信息
	// 	case 'GetAreaInfo':
	// 			$pdo = MyPdoShare::init();
	// 			$sql = 'select * from share_city';
	// 			$res = $pdo->dql($sql,array());
	// 			Merge_Or_Make_Cache($res,'Cache/City/city');
	// 		break;





	// 	//商户登陆时缓存县库信息
	// 	case 'SetDbInfo':
	// 		$db_info = I('db_info','post');
	// 		$store_id = I('store_id','post');
	// 		$store_name = I('store_name','post');
	// 		if(!$db_info || !$store_id || !$store_name){
	// 			exit('err2');
	// 		}
	// 		$_SESSION['CountyInfo'] = explode(',',$db_info);
	// 		if($_SESSION['CountyInfo'] && $_SESSION['CountyInfo'][2]){
	// 			$v_code = SetFieldsVCode($store_id,$store_name,$_SESSION['CountyInfo'][1],$_SESSION['CountyInfo'][2]);
	// 			$_SESSION['StoreVCode'] = $v_code;
	// 			if(@$_SESSION['StoreVCode']){
	// 				 exit('ok');
	// 			}else{
	// 				 exit('err3');
	// 			}
	// 		}else{
	// 			 exit('err');
	// 		}
	// 		break;



	// 	case 'QueryStoreStat':
	// 		$id = intval(I('id','post'));
	// 		$pdo = MyPdoCounty::init();
	// 		$sql = 'select stat from shop_store where id=?';
	// 		$res = $pdo->dql($sql,array($id),1);
	// 		if($res){
	// 			echo $res['stat'];
	// 		}
	// 		break;



	// 	case 'GetCityFullInfo':
	// 		$pdo = MyPdoShare::init();
	// 		if(I('open_type','post')){
	// 			$sql = "select id,code,name,parent_id,level,db_host,db_name from share_city where level=2 and db_host != '' and db_name != ''";
	// 		}else{
	// 			$sql = "select id,code,name,parent_id,level,db_host,db_name from share_city";
	// 		}
	// 		$res = $pdo->dql($sql,array());
	// 		if($res){
	// 			$res2 = json_encode($res);
	// 			$res3 = 'Gets('.$res2.')';
	// 			$path = '../Cache/CityInfo/update_stat';
	// 			if(I('open_type','post') && file_exists($path)){
	// 				UpyunAddFile($config,'CityInfo/CityOpen.html',$res3);
	// 				unlink($path);
	// 			}
	// 			exit($res3);
	// 		}else{
	// 			$res3 = 'Gets([])';
	// 			$path = '../Cache/CityInfo/update_stat';
	// 			if(I('open_type','post') && file_exists($path)){
	// 				UpyunAddFile($config,'CityInfo/CityOpen.html',$res3);
	// 				unlink($path);
	// 			}
	// 			exit('err');
	// 		}
	// 		break;







		default :
			echo 'default';
		break;

	}




